package cn.wolfcode.luowowo.comment.service.impl;

import cn.wolfcode.luowowo.comment.domain.PersonalCenter;
import cn.wolfcode.luowowo.comment.domain.TravelComment;
import cn.wolfcode.luowowo.comment.repository.PersonalCenterRepository;
import cn.wolfcode.luowowo.comment.service.IPersonalCenterService;
import cn.wolfcode.luowowo.common.result.Result;
import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;

import static cn.wolfcode.luowowo.common.result.StatusCode.RESULT_SUCCESS;


@Service
public class PersonalCenterServiceImpl implements IPersonalCenterService {

    @Autowired
    private PersonalCenterRepository repository;
    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public Result getByUserId(JSONObject json) {
        Result result = new Result();
        JSONObject returnData = new JSONObject();
//        Criteria userId = Criteria.where("userId").is(id);
        Long uid = json.getLong("userId");
        Integer type = json.getInteger("type");
        Criteria criteria = new Criteria().andOperator(
                Criteria.where("userId").is(uid),
                Criteria.where("type").is(type)
        );
        Query query = new Query();
        query.addCriteria(criteria);

        List<PersonalCenter> collects = mongoTemplate.find(query, PersonalCenter.class, "personal_center");

        returnData.put("list", collects);
        result.setResultCode(RESULT_SUCCESS);
        result.setResultData(returnData);
        return result;


    }
    //查询所有点评
    @Override
    public List<PersonalCenter> getcomment(Long userInfoId) {
        Criteria criteria = new Criteria().andOperator(
                Criteria.where("userId").is(userInfoId)
        );
        Query query = new Query();
        query.addCriteria(criteria);
        List<PersonalCenter> p = mongoTemplate.find(query, PersonalCenter.class, "personal_center");

        return p;
    }

    @Override
    public long getCount(Long userInfoId) {
        Query query = new Query();
        if (userInfoId != null){
            query.addCriteria(Criteria.where("userId").is(userInfoId));
        }
        long count = mongoTemplate.count(query, PersonalCenter.class,"personal_center");
        return count;
    }


}
